﻿using System;
using System.Data.SqlClient;
using System.Windows.Forms;


namespace Mes_app
{
    public partial class ChangePassword_Form : Form
    {
        public ChangePassword_Form()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            // this.ComboBox_UserID.SelectedValue = Login.login_user_id;
            //  this.ComboBox_UserID.SelectedText = Login.login_user_name;

            string textuserid = this.TextUserid.Text;
            string textusername = this.Label_userName.Text;
            string textpassword = TextPassWord.Text.ToString();
            string textpasswordok = TextPassWordOK.Text.ToString();


            if (textuserid.Equals(""))
            {
                MessageBox.Show("用户ID不能为空！");
                return;
            }

            if (textusername.Equals(""))
            {
                MessageBox.Show("用户名不能为空！");
                return;
            }
            if (textpassword.Equals(""))
            {
                MessageBox.Show("新密码不能为空！");
                return;
            }
            if (textpasswordok.Equals(""))
            {
                MessageBox.Show("确认密码不能为空！");
                return;
            }
            if (!textpassword.Equals(textpasswordok))
            {
                MessageBox.Show("密码和确认密码不一致！");
                return;
            }


            //查询userid是否存在
            string sqlValue = "SELECT count(*) FROM login_user WHERE login_user_id = @userid ";
            SqlParameter[] parameters = {
                  new SqlParameter("@userid",textuserid)
            };

            int count = (int)SqlHelper.ExecuteScalar(sqlValue, parameters);
            Console.WriteLine(count);

            if (count > 0)
            {
                string sqlinsert = "UPDATE login_user SET login_user_password = @textpassword WHERE login_user_id = @userid";
                SqlParameter[] param_insert = {
                  new SqlParameter("@userid",textuserid),
                  new SqlParameter("@textpassword",textpasswordok),
                };

                int count_id = SqlHelper.ExecuteNonQuery(sqlinsert, param_insert);
                label_99.Text = "用户密码" + count_id + "修改成功";
                
            }
            else
            {
                label_99.Text = "用户名不存在";
            }
        }


        private void ChangePassword_Load(object sender, EventArgs e)
        {
            this.TextUserid.Text = Login_Form.login_user_id;
            this.Label_userName.Text = Login_Form.login_user_name;

        }
    }
}
